<?php
/**
 * Video
 *
 * Description de la classe
 *
 * @author Valentin
 */
class VideoAssociation extends Object {
    protected $id;
    protected $association_id;
    protected $titre;
    protected $description;
    protected $file;
    protected $duree;
    protected $nbvue;
    protected $flag;
    protected $vignette;
    protected $added;

    protected $note;

     function __construct($id="", $params=array()) {
        if($id != "") {
            $this->id = $id;
            foreach($params as $key => $p) {
                $this->$key = $p;
            }
        }
    }

    function __toString() {
        if(!isset($this->titre)) $this->load();
        return $this->titre;
    }

    public function load() {
        $db = Database::getInstance();
        $sql = "SELECT v.id, v.titre, v.vignette, v.nbvue, AVG(n.note) as note, v.file
                FROM videos_associations v
                INNER JOIN notes_videos_associations n ON n.video_id = v.id
                WHERE v.id = '".$this->id."'";
        $result = $db->query($sql);
        if(sizeof($result) > 0) {
            foreach($result[0] as $key => $p) {
                $this->$key = $p;
            }
            return;
        }
        if(DEBUG) echo("Objet inconnu");
        return false;
    }

    public function getUrl() {
        if(!isset($this->titre) || !isset($this->theme)) $this->load();
        return URL_PATH."videos/".$this->theme."/".$this->id."/".$this->titre."/";
    }

    public function getNote() {
        if(!isset($this->note)) $this->load();
        return ceil($this->note);
    }

    public function getImgNote(){
        if(!isset($this->note)) $this->load();
        return IMG_PATH.'icones/note'.ceil($this->note).'.png';
    }

    public function getShortTitle() {
        if(!isset($this->titre)) $this->load();
        if(strlen($this->titre) > 25) return substr($this->titre, 0, 25)."...";
        return $this->titre;
    }

    public function getOverview(){
        return strip_tags($this->description);
    }

    public function getThumb(){
        if(!isset($this->vignette)) $this->load();
        return $this->vignette;
    }
}
?>
